while abs(wage_distance)>1e-2 && counter<=50    
    counter=counter+1;
    
    omega = wage_US / wage.guess_OECD;

    % ASSIGN EXPORTS AND IMPORTS BASED ON GUESSED RELATIVE WAGE
    relative_quality=qualities_vec_US ./ qualities_vec_OECD;
    ordered_quality=sort(relative_quality,'descend');

    exports_US = relative_quality>import_tariff_OECD*omega;
    exports_OECD = relative_quality*import_tariff_US<omega;
    
    x_guess = sum(exports_US)/n_varieties;
    m_guess = sum(exports_OECD)/n_varieties;

    produced_US = ~exports_OECD;
    produced_OECD = ~exports_US;
    
    nontraded_US = ~exports_US & ~exports_OECD;
    nontraded_OECD = nontraded_US;
    
    % TRACK COMPETITORS FOR AND CALCULATE PRICES AND MARKUPS
    next_best_qual_vec_US = max(domestic_competitor_qual_vec_US/wage_US,qualities_vec_OECD/(import_tariff_US*wage.guess_OECD));
    is_domestic_competitor_exports_US = domestic_competitor_qual_vec_US/(import_tariff_OECD*wage_US)>qualities_vec_OECD/(wage.guess_OECD);

    next_best_qual_vec_OECD = max(domestic_competitor_qual_vec_OECD/wage.guess_OECD,qualities_vec_US/(import_tariff_OECD*wage_US));
    is_domestic_competitor_exports_OECD = domestic_competitor_qual_vec_OECD/(import_tariff_US*wage.guess_OECD)>qualities_vec_US/(wage_US);

    prices_US = qualities_vec_US./ next_best_qual_vec_US;

    prices_US(exports_OECD & is_domestic_competitor_exports_OECD) = (qualities_vec_OECD(exports_OECD & is_domestic_competitor_exports_OECD) ./ ...
            domestic_competitor_qual_vec_OECD(exports_OECD & is_domestic_competitor_exports_OECD)) * import_tariff_US * wage.guess_OECD;

    prices_US(exports_OECD & ~is_domestic_competitor_exports_OECD) = (qualities_vec_OECD(exports_OECD & ~is_domestic_competitor_exports_OECD) ./ ...
            qualities_vec_US(exports_OECD & ~is_domestic_competitor_exports_OECD)) * wage_US;
        
    price_ceiling_US = (sigg)/(sigg-1) * ones(size(prices_US)) * wage_US;
    price_ceiling_US(exports_OECD) = price_ceiling_US(exports_OECD) * import_tariff_US;
    prices_US = min([prices_US price_ceiling_US],[],2);
    
    markups_US = prices_US / wage_US;
    markups_US(exports_OECD) = prices_US(exports_OECD) / (import_tariff_US * wage.guess_OECD);
    
    prices_OECD = qualities_vec_OECD./ next_best_qual_vec_OECD;

    prices_OECD(exports_US & is_domestic_competitor_exports_US) = (qualities_vec_US(exports_US & is_domestic_competitor_exports_US) ./ ...
            domestic_competitor_qual_vec_US(exports_US & is_domestic_competitor_exports_US)) * wage_US * import_tariff_OECD;

    prices_OECD(exports_US & ~is_domestic_competitor_exports_US) = (qualities_vec_US(exports_US & ~is_domestic_competitor_exports_US) ./ ...
            qualities_vec_OECD(exports_US & ~is_domestic_competitor_exports_US)) * wage.guess_OECD;

    price_ceiling_OECD = (sigg)/(sigg-1) * ones(size(prices_OECD)) * wage.guess_OECD;
    price_ceiling_OECD(exports_US) = price_ceiling_OECD(exports_US) * import_tariff_OECD;
    prices_OECD = min([prices_OECD price_ceiling_OECD],[],2);
    
    markups_OECD = prices_OECD / wage.guess_OECD;
    markups_OECD(exports_US) = prices_OECD(exports_US) / (import_tariff_OECD * wage_US);
    
    P_bar_US = (sum((prices_US(produced_US) ./ qualities_vec_US(produced_US)).^(1-sigg)) + sum((prices_US(exports_OECD) ./ qualities_vec_OECD(exports_OECD)).^(1-sigg)))^(1/(1-sigg));
    P_bar_OECD = (sum((prices_OECD(produced_OECD) ./ qualities_vec_OECD(produced_OECD)).^(1-sigg)) + sum((prices_OECD(exports_US) ./ qualities_vec_US(exports_US)).^(1-sigg)))^(1/(1-sigg));
    
    % CALCULATE RELATIVE WAGE IMPLIED BY BALANCED TRADE AND COMPARE TO
    % GUESS. ITERATE UNTIL THEY ARE THE SAME.
%     wage.balanced_trade_OECD = abs(((mu_bar.step7_US*wage_US*L_US*m_guess*import_tariff_OECD)/(x_guess*import_tariff_US)-...
%         (import_tariff_OECD-1) * export_share.step6_US * mu_bar.step7_US * wage_US * L_US)...
%         /(mu_bar.step7_OECD * L_OECD - (import_tariff_US-1) * (m_guess * import_tariff_OECD) / (x_guess * import_tariff_US) * export_share.step6_OECD * mu_bar.step7_OECD * L_OECD));
    wage.balanced_trade_OECD = abs(((mu_bar.step7_US*wage_US*L_US*((P_bar_US^(sigg-1)).*sum(1./(prices_US(exports_OECD)./qualities_vec_OECD(exports_OECD))))*import_tariff_OECD) ...
                                ./(((P_bar_OECD^(sigg-1)).*sum(1./(prices_OECD(exports_US)./qualities_vec_US(exports_US))))*import_tariff_US)-...
                                (import_tariff_OECD-1) * export_share.step6_US * mu_bar.step7_US * wage_US * L_US)...
                                /(mu_bar.step7_OECD * L_OECD - (import_tariff_US-1) * (((P_bar_US^(sigg-1)).*sum(1./(prices_US(exports_OECD)./qualities_vec_OECD(exports_OECD)))) * import_tariff_OECD) ...
                                / (((P_bar_OECD^(sigg-1)).*sum(1./(prices_OECD(exports_US)./qualities_vec_US(exports_US)))) * import_tariff_US) * export_share.step6_OECD * mu_bar.step7_OECD * L_OECD));

    wage_distance = wage.balanced_trade_OECD - wage.guess_OECD;
   
  
        wage.guess_OECD = wage.guess_OECD + (0.05*wage_distance);

   
   if counter>950
     wage_bounce_list = [wage_bounce_list; wage.guess_OECD];
   end 

end
 % IF THE ITERATION DOESN'T CONVERGE TO THE TOLERANCE LEVEL, TAKE THE WAGE
 % IMPLIED BY THE LAST 50 WAGE GUESSES THAT BOUNCED AROUND THE EQUILIBRIUM
if counter==1000
    wage.balanced_trade_OECD=mean(wage_bounce_list);
end